#!/bin/sh
################################################

# . /etc/init.d/functions

# ----------------分区---------------------
fdisk_fun() {
# fdisk -S 56 $1 << EOF
fdisk  $1 << EOF
n
p
${2}


wq
EOF

sleep 5
echo "----同步磁盘表------"
partprobe
echo "----- 格式化成ext4-----"
# mkfs.xfs -f ${1}${2}
mkfs.ext4  ${1}${2}
}



# ------------交互的方式获得硬盘+是否分区+挂载路径-----------------
get_input(){
echo "--------------step1： 获取要挂载硬盘标识-----------"
			echo "1.1 本机现有的硬盘为:"
			fdisk -l 2> /dev/null | grep -o "/dev/[vsh]d[a-z]"| uniq
			echo ""
			echo "1.2 本机现有的所有分区为："
			lsblk
			echo ""
			#------------获取要挂载的硬盘名-------------------
			while true
			do
				read -p "1.3  请输入需要挂载的硬盘名，比如/dev/sda，如需退出，输入q："  disk
				[ -z "$disk" ] && disk="/dev/sda"
				if [ $disk =  "q" ]; then
					return 1
					break
				elif [ -z "`echo $disk | grep '^/dev/'`" ]; then
						echo "输入有误，应该以 /dev 开头"
						continue						
				fi
				
				echo "1.4 ==========info：需要挂载的硬盘为$disk=========="
				break
			done
  	echo ""
  	echo ""
			# ----------获取要挂载的路径-------------------
echo "--------------step2： 获取要挂载的路径-----------"			
			while true	
			do
				path=/cetc/data1	
				echo "2.1 请输入需要挂载的绝对路径，默认为$path	，不过避免输入${path%/*}中已存在的目录:"
				existpath=`ls  ${path%/*}`
				echo "${path%/*}/{$existpath}"
				read -p "请正式输入，默认为$path	，如需退出请输入q：" mount_dir
				# 如果无输入，则设定默认值
				[ -z "$mount_dir" ] && mount_dir=/cetc/data1
				## 对输入进行判断
				if [ $mount_dir  =  "q" ]; then
					return 1
					break
				elif [ -e "$mount_dir" ];then
					echo "本地目录 $mount_dir 已存在，请手动迁移 " 
					return 1
					break
				elif 	[ ! -e "$mount_dir" ];then
					mkdir -p $mount_dir
					chmod -R 777 $mount_dir
					echo "$mount_dir不存在，已新建目录"
					
				elif [ -z "`echo $mount_dir | grep '^/'`" ]; then
						echo "输入有误，应该以 / 开头 "
						continue
				fi
				echo "2.3 ==========info：需要挂载的目录为$mount_dir========="
				break
			done
			sleep 1
			echo ""
			echo ""
echo "--------------step3： 获取分区信息-----------"
			# -------------是否要分区------------
			while true
			do
				echo "3.1 现有的分区情况为"
				lsblk
				read -p "3.2  输入需要分的分区，默认为0，若为0则不分区，如需退出，输入q：" partdisk
				# 默认值
				[ -z "$partdisk" ] && partdisk=0
				if [ $partdisk =  "q" ]; then
					return 1
					break
				elif [ -z "`echo $partdisk | grep '[0-9]'`" ]; then
						echo "输入错误，分区数应该为数字 "
						continue
				fi
				echo "3.3 ==========info：新的分区为$disk$partdisk========="
				break
			done
			echo ""
			echo ""
			sleep 2
}


manual_fdisk(){
			# 挂载磁盘
		echo "**********1 交互获得分区信息*************"	
		get_input
		if [ $? -ne 0  ];then
		  echo "主动中断或者意外退出"
				# clear
		  return
		fi
		echo "************2 确认信息************"
		echo "|info：需要挂载的硬盘为$disk|"
		echo "|info：需要挂载的目录为$mount_dir|"
		if [ $partdisk -ne  0  ];then
			echo "|info：新的分区为$disk$partdisk|"
		else 
			echo "|info：新的分区为$disk|"
		fi	
		read -p "确认是否开始分区请输入yes，默认为yes:" affirm
		[ -z "$affirm" ] && affirm=yes
		if [ $affirm != "yes" ];then
		  return
		fi
		
		echo "*************开始分区*************"
			export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
			# 分区
			if [ $partdisk -ne  0  ];then
			 echo "-------构建分区：$disk$partdisk，并格式化-------------"
				fdisk_fun $disk $partdisk
				echo "--------将$disk$partdisk 挂载到$mount_dir上"
				mount $disk$partdisk $mount_dir
				uuid=`blkid -s UUID  $disk$partdisk | awk -v FS="\"" '{print $2}'`
				echo "----------将uuid:$uuid写入 /etc/fstab-----------"
				# echo "UUID=$uuid    $mount_dir   ext4     noatime,nodiratime,nobarrier,logbufs=8   0    2" >> /etc/fstab
				echo "UUID=$uuid    $mount_dir   ext4    defaults   0    2" >> /etc/fstab
				# # echo "$disk$partdisk $mount_dir ext4  noatime,nodiratime,nobarrier,logbufs=8 0 2" >> /etc/fstab
				# # echo "$disk$partdisk $mount_dir xfs  noatime,nodiratime,nobarrier,logbufs=8 0 2" >> /etc/fstab
			# 无分区	，则全盘格式化
			else
			 echo "---将$disk 格式化成ext4-----"
				mkfs.ext4  $disk
				echo "--------将$disk挂载到$mount_dir上----------" 
				mount $disk $mount_dir
			 uuid=`blkid -s UUID  $disk | awk -v FS="\"" '{print $2}'`
			 echo "-------将uuid: $uuid 写入/etc/fstab，开机启动---------"
				# echo "UUID=$uuid    $mount_dir    ext4    noatime,nodiratime,nobarrier,logbufs=8   0    2" >> /etc/fstab
				echo "UUID=$uuid    $mount_dir    ext4    defaults   0    2" >> /etc/fstab
				# # echo "$disk $mount_dir xfs  noatime,nodiratime,nobarrier,logbufs=8 0 2" >> /etc/fstab
				# # echo "$disk $mount_dir ext4  noatime,nodiratime,nobarrier,logbufs=8 0 2" >> /etc/fstab
			fi
			# # echo "$disk$partdisk $mount_dir xfs  noatime,nodiratime,nobarrier,logbufs=8 0 2" >> /etc/fstab
			echo ""
			echo "==============查看挂载情况=============="
			lsblk
			echo ""
			echo "==============查看 /etc/fstab============="
			tail -n 4 /etc/fstab
			# action "挂载硬盘成功" /bin/true
			echo ""
			sleep 2
}
# 执行挂载

manual_fdisk
